package com.solcoo.merchant.manager;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import com.solcoo.common.App;
import com.solcoo.common.util.StringUtil;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.model.User;
import com.solcoo.merchant.ui.login.LoginActivity;
import com.solcoo.merchant.ui.menu.MenuActivity;

public class UserManager {

	private static UserManager instance;

	private static User mUser;

	private UserManager() {
	}

	public static UserManager getInstance() {
		if (instance == null) {
			instance = new UserManager();
		}
		return instance;
	}

	public User getUser() {
		if (mUser == null) {
			SharedPreferences localSharedPref = App.CONTEXT
					.getSharedPreferences(Constant.SharedKey.USER,
							Context.MODE_PRIVATE);
			mUser = loadFromSharedPref(localSharedPref);
		}
		return mUser;
	}

	public void setUser(User user) {
		if (user == null) {
			clearUserInfo();
		} else {
			saveToSharedPref(user);
		}
		mUser = user;
	}

	private User loadFromSharedPref(SharedPreferences localSharedPref) {
		User user = new User();
		user.yonghum = localSharedPref.getString("memberNo", StringUtil.EMPTY);
		user.shoujih = localSharedPref.getString("phoneNo", StringUtil.EMPTY);
		user.touxiang = localSharedPref.getString("touxiang", StringUtil.EMPTY);
		user.xingbie = localSharedPref.getInt("xingbie", User.MALE);
		user.nicheng = localSharedPref.getString("nicheng", StringUtil.EMPTY);
		if (StringUtil.isBlank(user.yonghum)
				|| StringUtil.isBlank(user.shoujih)) {
			return null;
		}
		return user;
	}

	private void saveToSharedPref(User user) {
		if (user != null) {
			SharedPreferences localSharedPreferences = App.CONTEXT
					.getSharedPreferences(Constant.SharedKey.USER,
							Context.MODE_PRIVATE);
			SharedPreferences.Editor localEditor = localSharedPreferences
					.edit();

			localEditor.putString("memberNo",
					StringUtil.defaultIfBlank(user.yonghum));
			localEditor.putString("phoneNo",
					StringUtil.defaultIfBlank(user.shoujih));
			localEditor.putString("nicheng",
					StringUtil.defaultIfBlank(user.nicheng));
			localEditor.putInt("xingbie", user.xingbie);
			localEditor.putString("touxiang",
					StringUtil.defaultIfBlank(user.touxiang));
			localEditor.commit();
		}
	}

	private void clearUserInfo() {
		SharedPreferences localSharedPreferences = App.CONTEXT
				.getSharedPreferences(Constant.SharedKey.USER,
						Context.MODE_PRIVATE);
		SharedPreferences.Editor localEditor = localSharedPreferences.edit();
		localEditor.clear();
		localEditor.commit();
	}

	public void loginSuccess(Dialog dialog, Context context, String msg) {
		// if (dialog == null) {
		// dialog = DialogUtil.createDialog(context,
		// new LoginSuccessOnClickListener(context), null, null, msg,
		// null);
		// }
		// dialog.show();
		Intent it = new Intent(context, MenuActivity.class);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(it);
	}

	private class LoginSuccessOnClickListener implements
			android.content.DialogInterface.OnClickListener {

		private Context mContext;

		public LoginSuccessOnClickListener(Context context) {
			mContext = context;
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Intent it = new Intent(mContext, MenuActivity.class);
			it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			mContext.startActivity(it);
		}
	};

	public void logout(Context context) {
		setUser(null);
		Intent it = new Intent(context, LoginActivity.class);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(it);
	}
}
